import datetime
from typing import List
from pydantic import Field, BaseModel, root_validator
from package.fastapi.schema import Page, BatchId


class EditDevops(BaseModel):
    """修改记录"""
    category: str = Field(None, title='类型')
    description: str = Field(..., title='描述')
    host: str = Field(..., title='主机')
    port: int = Field(None, title='端口')
    configure: dict = Field(None, title='配置')
    heartbeat_interval: int = Field(None, title='心跳监测周期')
    metric_interval: int = Field(None, title='指标监测周期')
    extra: dict = Field(None, title='额外配置')


class CreateDevops(EditDevops):
    """创建记录"""
    name: str = Field(..., title='名称')


class DevopsOut(CreateDevops):
    """返回数据不返回密码"""
    id: str = Field(..., title='记录ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        extra = 'allow'
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageDevopss(Page):
    """获取页码条目"""
    data: List[DevopsOut] = Field(..., title='')
